package cards;

import java.util.ArrayDeque;
import java.util.Collection;
import java.util.Deque;
import java.util.NoSuchElementException;

/**
 * Class DiscardPile represents the cards that have been discarded from 
 *      players' hands
 * @author uni
 */
public class DiscardPile {
    /** Acts as a stack (LIFO) - last (top) card is removed first */
    
    //Deque for discarded cards
    private Deque<Card> discardedCards = new ArrayDeque<>();
    
    /**
     * Adds a card to the top of the discard pile
     * @param newCard card to be added
     */
    public void addCard(Card newCard) {
        discardedCards.addFirst(newCard);
    }
    
    /**
     * Removes a card from the top of the discard pile
     * @return removed card
     * @throws NoSuchElementException if the discard pile is empty
     */
    public Card removeCard() throws NoSuchElementException {
        return discardedCards.removeFirst();
    }
    
    /**
     * Retrieves, but does not remove, the top card of the discard pile
     * @return card on the top, or null if the discard pile is empty
     */
    public Card getCard() {
        return discardedCards.peekFirst();
    }
    
    /**
     * Removes from the discard pile all cards except the top one.
     * @return the collection of removed cards
     */
    public Collection<? extends Card> removeAllExceptTop() {
        Card cardToKeep = removeCard();
        Collection<? extends Card> colToReturn = discardedCards;
        discardedCards = new ArrayDeque<>();
        discardedCards.add(cardToKeep);
        return colToReturn;        
    }
    
}
